package com.wms.controller;

import com.mes.entity.User;
import com.wms.service.OutofstockService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("OutConfirm")
public class OutofstockController {
	@Autowired
	private OutofstockService outofstockservice;
	
	 
	//加载所有领料出库明细(不包含大底的)
	@ResponseBody
	@RequestMapping(value="OutConfirmList",method= RequestMethod.POST)
	public Message outConfirmList(HttpServletRequest request){
		Message msg = new Message();

		String ok = "NO";
			
		msg.setData("Stocks",outofstockservice.getSparepartsList(ok));
		
		
		return msg;
	}

	//加载所有领料出库明细（带查询条件的）(不包含大底的)
	@ResponseBody
	@RequestMapping(value="OutConfirmLists",method= RequestMethod.POST)
	public Message outConfirmLists(HttpServletRequest request){
		Message msg = new Message();

		String ok = "NO";
		String llcode = request.getParameter("llcode").toString();   //领料单号
		String COLORID = request.getParameter("colors").toString();   //颜色名称
		String wlname = request.getParameter("wlname").toString();   //物料名称
		String SIZEID = request.getParameter("sizes").toString();   //尺码code

		msg.setData("Lists",outofstockservice.getSparepartsLists(llcode,COLORID,SIZEID,ok, wlname));


		return msg;
	}

	//加载所有领料出库明细(大底的)
	@ResponseBody
	@RequestMapping(value="dadiOutConfirmList",method= RequestMethod.POST)
	public Message dadiOutConfirmList(HttpServletRequest request){
		Message msg = new Message();

		String ok = "YES";
		msg.setData("Stocks",outofstockservice.getSparepartsList(ok));


		return msg;
	}

	//加载所有领料出库明细（带查询条件的）(大底的)
	@ResponseBody
	@RequestMapping(value="dadiOutConfirmLists",method= RequestMethod.POST)
	public Message dadiOutConfirmLists(HttpServletRequest request){
		Message msg = new Message();

		String ok = "YES";
		String llcode = request.getParameter("llcode").toString();   //物料编码
		String COLORID = request.getParameter("colors").toString();   //颜色名称
		String SIZEID = request.getParameter("sizes").toString();   //尺码code
		String wlname = request.getParameter("wlname").toString();   //物料名称

		msg.setData("Lists",outofstockservice.getSparepartsLists(llcode,COLORID,SIZEID,ok,wlname));


		return msg;
	}


	
	//加载库存数量
		@ResponseBody
		@RequestMapping(value="SumQtys",method= RequestMethod.POST)
	public Message sumQtys(HttpServletRequest request){
		Message msg = new Message();
					
		    String WLID = request.getParameter("WLID").toString();   //领用的物料id
			String COLORID = request.getParameter("COLORID").toString();   //领用的颜色ID
			String SIZEID = request.getParameter("SIZEID").toString();   //领用的尺码ID

			User user = UserUtil.getLoginUser(request);

			String userIdtime = user.getName();			//登录人的名字

			//查询当前物料的库存数量
		msg.setData("sumnumbers",   outofstockservice.getSumMateriels(WLID,SIZEID,COLORID));	//查询当前物料的库存数量
		msg.setData("userIdtime",   userIdtime);

		return msg;
	}
	/*//修改领料明细大底RFID绑定数量及修改状态
	@ResponseBody
	@RequestMapping(value="updateJobStatus",method= RequestMethod.POST)
	public Message updateJobStatus(HttpServletRequest request){
		Message msg = new Message();

		try{
			String LLIDS  = request.getParameter("LLID"); //领料单ID
			String MXQTYS  = request.getParameter("MXQTYS").toString(); //领用数量
			String BDQTY  = request.getParameter("BDQTY").toString(); //已绑定数量
			String WLIDS = request.getParameter("WLID").toString();   //领用的物料id
			String SIZEIDS = request.getParameter("CMID").toString();   //领用的尺码ID
			String COLORIDS = request.getParameter("YSID").toString();   //领用的颜色ID
		//	String LEFTS = request.getParameter("LEFTS").toString();   //左右脚
			String DDCODE = "";


				List<Map<String,Object>> size =  outofstockservice.getProductSize(SIZEIDS);

				String sizeCode = size.get(0).get("CMCODE").toString();

				List<Map<String,Object>> wltype = outofstockservice.getProductType(WLIDS);

				String productType = wltype.get(0).get("FLVALUE").toString();
				String WLCODE = wltype.get(0).get("WLCODE").toString();

				List<Map<String,Object>> LISTKCQTYS = outofstockservice.getSumMateriels(WLIDS,SIZEIDS,COLORIDS);
				if( LISTKCQTYS.size() !=  0 ){
					String BATCHID = LISTKCQTYS.get(0).get("BATCHID").toString(); //入库批次ID

					String type = "大底";
					User user = UserUtil.getUser();
					String fw = "";
					String foottype = "";
					String Batch_No = GenerateCodeUtil.getCode("Wms_Storage_Batch_Item", "Description");  //大底生成对应批次

//					if(LEFTS.equals("左脚")){
//						fw = "L";
//						foottype = "left";
//					}else{
//						fw = "R";
//						foottype = "right";
//					}

					String kuanshi =  WLCODE.substring(4,6);
					//DDCODE = "S"+kuanshi+"-"+sizeCode+"-"+Batch_No+fw;
					DDCODE = "S"+kuanshi+"-"+sizeCode+"-"+Batch_No;

						outofstockservice.inStorageItem(BATCHID , type,  DDCODE , user,foottype);


					if(BDQTY == null || BDQTY.equals("")){
						BDQTY = "0";
					}
					int llqty = Integer.parseInt(MXQTYS);

					int aa = Integer.parseInt(BDQTY);
					int ybdqty =  aa +1;

					*//*Double llqty = Double.parseDouble(MXQTYS);

					Double aa = Double.parseDouble(BDQTY);
					Double ybdqty =  aa + 0.5;*//*
					if(llqty == ybdqty){
						outofstockservice.updateStorageMxListStatus(LLIDS,ybdqty);
					}else if(llqty > ybdqty){
						outofstockservice.updateStorageMxList(LLIDS,ybdqty);
					}
					msg.setData("RFIDCODE",DDCODE);
				}else{
					msg.setData("product",WLCODE);
					msg.setData("results","oldresults");
					return msg;
				}

		}catch (Exception e){
			msg.setData("results","false");

			e.printStackTrace();
		}
		return msg;
	}*/
	
	//处理领料结果
	@ResponseBody
	@RequestMapping(value="SaveOutConfirmLists",method= RequestMethod.POST)
	public Message outConfirmResult(HttpServletRequest request){
		Message msg = new Message();

		try {


			String WLIDS = request.getParameter("WLIDS").toString();   //领用的物料id
			String COLORIDS = request.getParameter("COLORIDS").toString();   //领用的颜色ID
			String SIZEIDS = request.getParameter("SIZEIDS").toString();   //领用的尺码ID

			String DWIDS = request.getParameter("DWIDS").toString();   //领用的物料单位id
			String MXQTYS = request.getParameter("MXQTYS").toString();   //
			String CKIDS = request.getParameter("CKIDS").toString();   //领用的仓库ID
			String LLIDS = request.getParameter("LLIDS").toString();   //领用的领料单ID
			String LLCODES = request.getParameter("LLCODES").toString();   //领用的领料单ID
			String LISTIDS= request.getParameter("LISTIDS").toString();   //领用的领料单ID

			String [] WLID = WLIDS.split(",");
			String [] COLORID = COLORIDS.split(",");
			String [] SIZEID = SIZEIDS.split(",");
			String [] DWID = DWIDS.split(",");
			String [] MXQTY = MXQTYS.split(",");
			String [] CKID = CKIDS.split(",");
			String [] LLID = LLIDS.split(",");
			String [] LLCODE = LLCODES.split(",");
			String [] LISTID = LISTIDS.split(",");

			int numbers = WLID.length;

			User user = UserUtil.getLoginUser(request);

			String adorgId = user.getOrgId();         //登录人的ad_org_id
			String adclientId = user.getClientId();   //登录人的ad_client_id
			String userIdtime = user.getId();			//登录人的ad_user_id
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



			Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间

			List<Map<String, Object>> ListUser = outofstockservice.getEquipmentputCreateby(userIdtime);

			String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy

			String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy

			for(int i = 0 ; i< numbers ; i++) {
				String ProductID = WLID[i];
				String YSID = COLORID[i];
				String CMID = SIZEID[i];
				String UOMID = DWID[i];
				String QTYS = MXQTY[i];
				String HOUSEID = CKID[i];
				String INTESTID = LLID[i];
				String CODE = LLCODE[i];
				String MXID= LISTID[i];

				double qty = Double.parseDouble(QTYS);


				List<Map<String,Object>> wltype = outofstockservice.getProductType(ProductID);

				String productType = wltype.get(0).get("FLVALUE").toString();
				String WLCODE = wltype.get(0).get("WLCODE").toString();

				List<Map<String,Object>> LISTKCQTYS = outofstockservice.getSumMateriels(ProductID,CMID,YSID);

				if( LISTKCQTYS.size() !=  0 ){
					String StockQtys = LISTKCQTYS.get(0).get("QTYS").toString();  //库存数量

					double stockqty = Double.parseDouble(StockQtys);

					String BATCHID = LISTKCQTYS.get(0).get("BATCHID").toString(); //入库批次ID

					String types = "领料出库";
					String status = "OK";

					if(qty <= stockqty){

						outofstockservice.updateStorageMx(MXID);

						outofstockservice.saveStorageLists(BATCHID,HOUSEID,-qty,UOMID,types,CODE,INTESTID,status,adorgId,adclientId,createBy,updatedBy,time);

					}else{
						msg.setData("product",WLCODE);
						msg.setData("results","last1");
						return msg;
					}
				}else{
					msg.setData("product",WLCODE);
					msg.setData("results","oldresults");
					return msg;
				}

			}



		}catch (Exception e){
			msg.setData("results","false");
			e.printStackTrace();
		}

		return msg;

	}
	

  		
  		
}
	
	
	
	
